<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * model 基类
 */
class MY_Model extends CI_Model {

	// 当前表
	var $table = NULL;
	var $row = NULL;
	var $result = NULL;
	var $num_rows = 0;
	var $result_num = 0;

	function __construct() {
		parent::__construct();
	}

	///////////////////////////////////////基础方法///////////////////////////////////////

	/**
	 * 新增数据
	 */
	function insert($data, $exp = array()) {
		$this->db->trans_start();

		foreach ($data as $key => $val) {
			if (!empty($exp) && in_array($val, $exp)) {
				$this->db->set($key, $val, FALSE);
			} else {
				$this->db->set($key, $val);
			}
		}

		$this->db->insert($this->table);
		$ret = $this->db->insert_id();

		$this->db->trans_complete();
		return $ret;
	}

	/**
	 * 修改数据
	 */
	function update($data, $where, $exp = array()) {
		$this->db->trans_start();

		foreach ($data as $key => $val) {
			if (!empty($exp) && in_array($val, $exp)) {
				$this->db->set($key, $val, FALSE);
			} else {
				$this->db->set($key, $val);
			}
		}

		foreach ($where as $k => $v) {
			if (is_array($v)) {
				$this->db->where_in($k, $v);
			} else {
				$this->db->where($k, $v);
			}
		}
		$this->db->update($this->table);
		$ret = $this->db->affected_rows();
		$this->db->trans_complete();
		return $ret;
	}

	/**
	 * 刪除数据
	 */
	function delete($where) {
		$this->db->trans_start();

		foreach ($where as $k => $v) {
			if (is_array($v)) {
				$this->db->where_in($k, $v);
			} else {
				$this->db->where($k, $v);
			}
		}
		$ret = $this->db->delete($this->table);

		$this->db->trans_complete();
		return $ret;
	}

	///////////////////////////////////////获取指定数据///////////////////////////////////////

	/**
	 * 获取数据集
	 */
	function get_result($where = array(), $config = array()) {

		$select = '*';
		$limit = NULL;
		$offset = NULL;
		$join = NULL;
		$field = NULL;
		$order_by = NULL;
		$this->result = NULL;

		if (is_array($config) && count($config) > 0) {
			foreach ($config as $key => $val) {
				$$key = $val;
			}
		}

		if (count($where) > 0) {
			foreach ($where as $k=>$val) {
				if(is_array($val)) {
					$this->db->where_in($k, $val);
				} else {
					$this->db->where($k, $val);
				}
			}
		}
		
		if ($field && $order_by) {
			$this->db->order_by($field, $order_by);
		}

		if ($limit && $offset >= 0) {
			$this->db->limit($limit, $offset);
		}
		
		if($join) {
			foreach ($join as $v) {
				$this->db->join($v[0], $v[1], $v[2]);
			}
		}
		$query = $this->db->select($select)->from($this->table)->get();
		$this->result = $query->result_array();
		$query->free_result();
		return $this->result;
	}

	/**
	 * 獲取結果數量
	 */
	function get_num($where = array(), $config = array()) {
		$select = '*';
		$field = NULL;
		$order_by = NULL;
		$this->result = NULL;

		if (is_array($config) && count($config) > 0) {
			foreach ($config as $key => $val) {
				$$key = $val;
			}
		}

		if (count($where) > 0) {
			foreach ($where as $k=>$val) {
				if(is_array($val)) {
					$this->db->where_in($k, $val);
				} else {
					$this->db->where($k, $val);
				}
			}
		}

		if ($field && $order_by) {
			$this->db->order_by($field, $order_by);
		}

		$this->num_rows = $this->db->select($select)->from($this->table)->count_all_results();
		return $this->num_rows;
	}

	/**
	 * 获取单条数据
	 */
	function get_row($where = array(), $select = '*', $config = array(), $protect=true) {

		$join = NULL;
		$this->row = NULL;
		
		if (!empty($config)) {
			foreach ($config as $key => $val) {
				$$key = $val;
			}
		}
		
		if (count($where) > 0) {
			foreach ($where as $k=>$val) {
				if(is_array($val)) {
					$this->db->where_in($k, $val);
				} else {
					$this->db->where($k, $val);
				}
			}
		}

		if($join) {
			foreach ($join as $v) {
				$this->db->join($v[0], $v[1], $v[2]);
			}
		}
		
		$query = $this->db->select($select,$protect)->get($this->table, 1);
		$this->row = $query->row_array();
		$query->free_result();
		return $this->row;
	}

	/**
	 * 获取数据集
	 */
	function search($where = array(), $config = array(), $like = array(), $protect=true, $tableAlias='') {
		$select = '*';
		$limit = NULL;
		$offset = NULL;
		$field = NULL;
		$order_by = NULL;
		$join = NULL;
		$group_by = NULL;
		$this->result = NULL;

		if (!empty($config)) {
			foreach ($config as $key => $val) {
				$$key = $val;
			}
		}

		if (!empty($where)) {
			$this->db->where($where);
		}
		if (!empty($like)) {
			foreach ($like as $k=>$v) {
				if(strpos($k, '|') !== FALSE && $v != '') {
					$this->db->like(str_replace('|', '.', $k), $v);
				}elseif(strpos($k, '=') !== FALSE && $v != '') {
					$this->db->where(str_replace('=', '.', $k), $v);
				}
			}
			$raw = $this->db->field_data($this->table);
			foreach ($raw as $val) {
				$alias = trim($tableAlias);
				$alias .= empty($alias) ? '' : '.';
				if (!isset($like[$val->name]) || $like[$val->name] == '') {
					continue;
				}
				if (in_array($val->type, array('varchar', 'text'))) {
					$this->db->like($alias . $val->name, $like[$val->name]);
				} else {
					$this->db->where($alias . $val->name, $like[$val->name]);
				}
			}
		}

		if ($field && $order_by) {
			$this->db->order_by($field, $order_by);
		}

		if ($limit && $offset >= 0) {
			$this->db->limit($limit, $offset);
		}

		$this->db->select($select, $protect)->from($this->table . $tableAlias);
		
		if($join) {
			foreach($join as $v) {
				$this->db->join($v[0], $v[1], $v[2]);
			}
		}
		
		if($group_by) {
			$this->db->group_by($group_by);
		}
		$query = $this->db->get();

		$this->result = $query->result_array();
		$query->free_result();
		return $this->result;
	}

	/**
	 * 获取结果数量
	 */
	function get_search_num($where = array(), $like = array(), $config=array(), $tableAlias = '') {
		$joinSwitch = true;
		$join = NULL;
		$group_by = NULL;
		$select = NULL;
		$this->result = NULL;

		if (!empty($config)) {
			foreach ($config as $key => $val) {
				$$key = $val;
			}
		}
		$this->result = NULL;

		if (!empty($where)) {
			$this->db->where($where);
		}

		if (!empty($like)) {
			foreach ($like as $k=>$v) {
				if(strpos($k, '|') !== FALSE && $v != '') {
					$joinSwitch = true;
					$this->db->like(str_replace('|', '.', $k), $v);
				}elseif(strpos($k, '=') !== FALSE && $v != '') {
					$joinSwitch = true;
					$this->db->where(str_replace('=', '.', $k), $v);
				}
			}
			$raw = $this->db->field_data($this->table);
			foreach ($raw as $val) {
				if (!isset($like[$val->name]) || $like[$val->name] == '') {
					continue;
				}
				if (in_array($val->type, array('varchar', 'text'))) {
					$this->db->like($val->name, $like[$val->name]);
				} else {
					$this->db->where($val->name, $like[$val->name]);
				}
			}
		}
		$this->db->from($this->table . $tableAlias);
		if($joinSwitch && $join) {
			foreach($join as $v) {
				$this->db->join($v[0], $v[1], $v[2]);
			}
		}
		
		if($joinSwitch && $group_by) {
			$this->db->select($group_by)->distinct();
		}
		$this->num_rows = $this->db->count_all_results();
		return $this->num_rows;
	}

}

// END MY_Model Class
